package com.bbh.plugin.single.privilege.core.service;


import com.bbh.plugin.single.privilege.core.entity.LanguageInfo;
import com.bbh.plugin.single.privilege.core.entity.LanguageInfoLabel;
import com.bbh.plugin.single.privilege.core.repository.LanguageInfoMapper;
import com.bbh.common.domain.Condition;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.bbh.common.domain.Page;


import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.UUID;
import com.bbh.common.base.service.BaseService;
@Service
@Transactional
public class LanguageInfoService extends BaseService<LanguageInfo, String> {
    @Autowired
    private LanguageInfoMapper languageInfoMapper;
    @Autowired
    private LanguageInfoLabelService languageInfoLabelService;

    public Page<LanguageInfo> selectPage(Map<String, Object> searchParams, Page pageable) {
        PageHelper.startPage(pageable.getPageNum() + 1, pageable.getPageSize());
        List<LanguageInfo> languageInfoVoList = languageInfoMapper.selectList(searchParams);
        PageInfo<LanguageInfo> pageInfo = new PageInfo<LanguageInfo>(languageInfoVoList);
        //Page<LanguageInfo> page = new PageImpl<LanguageInfo>(pageInfo.getList(), pageable, pageInfo.getTotal());
        return null;
    }

    public void insert(LanguageInfo languageInfo, String labels) {
        super.insert(languageInfo);
        if (StringUtils.isNotEmpty(labels) && labels != "") {
            //先删除
            LanguageInfoLabel oldInfoLabel = new LanguageInfoLabel();
            Condition condition = new Condition();
            condition.putEquals("languageInfoId", languageInfo.getId());
            List<LanguageInfoLabel> labelList = languageInfoLabelService.selectList(condition, null);
            if (CollectionUtils.isNotEmpty(labelList)) {
                for (LanguageInfoLabel infoLabel : labelList) {
                    languageInfoLabelService.deleteByPrimaryKey(infoLabel.getId());
                }
            }
            String[] label = labels.split(",");
            LanguageInfoLabel languageInfoLabel = new LanguageInfoLabel();
            for (String labelId : label) {
                languageInfoLabel.setId(UUID.randomUUID().toString().replace("-", ""));
                languageInfoLabel.setLanguageInfoId(languageInfo.getId());
                languageInfoLabel.setLanguageLabelId(labelId);
                languageInfoLabelService.insert(languageInfoLabel);
            }
        }
    }

    public void update(LanguageInfo languageInfo, String labels) {
        languageInfoMapper.updateByPrimaryKeySelective(languageInfo);
        if (StringUtils.isNotEmpty(labels) && labels != "") {
            //先删除
            Condition condition = new Condition();
            condition.putEquals("languageInfoId", languageInfo.getId());
            List<LanguageInfoLabel> labelList = languageInfoLabelService.selectList(condition, null);
            if (CollectionUtils.isNotEmpty(labelList)) {
                for (LanguageInfoLabel infoLabel : labelList) {
                    languageInfoLabelService.deleteByPrimaryKey(infoLabel.getId());
                }
            }
            String[] label = labels.split(",");
            LanguageInfoLabel languageInfoLabel = new LanguageInfoLabel();
            for (String labelId : label) {
                languageInfoLabel.setId(UUID.randomUUID().toString().replace("-", ""));
                languageInfoLabel.setLanguageInfoId(languageInfo.getId());
                languageInfoLabel.setLanguageLabelId(labelId);
                languageInfoLabelService.insert(languageInfoLabel);
            }
        }
    }
}
